﻿<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="entityObj"
			:rules="rules"
			:disabled="mode == 'Show'"
			ref="dialogForm"
			label-width="100px"
		>
			<el-row
				><el-col :span="12"
					><el-form-item label-width="120px" label="编号" prop="Code"
						><el-input
							v-model="entityObj.Code"
							disabled /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="角色名"
						prop="Name"
						><el-input
							v-model="
								entityObj.Name
							" /></el-form-item></el-col></el-row
			><el-form-item label-width="120px" label="说明" prop="Remark"
				><el-input
					v-model="entityObj.Remark"
					ty="textarea"
					rows="4" /></el-form-item
			><el-row
				><el-col :span="12"
					><el-form-item label-width="120px" label="状态"
						><el-switch
							v-model="entityObj.State"
							active-value="1"
							inactive-value="0" /></el-form-item></el-col
			></el-row>
		</el-form>
		<template #footer>
			<el-input-number
				v-if="mode == 'Add' && addNum > 1"
				style="width: 120px"
				min="1"
				max="100"
				step="1"
				step-strictly="true"
				v-model="addNum"
			/>
			&nbsp;
			<el-button @click="visible = false">取 消</el-button>
			<el-button
				v-if="mode != 'show'"
				type="primary"
				:loading="isSaveing"
				@click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import publicjs from "@/api/model/publicjs";
export default {
	emits: ["success", "closed"],
	data() {
		return {
			props1: {
				checkStrictly: true,
				emitPath: false,
			},
			mode: "Add",
			titleMap: {
				Add: "新增",
				Edit: "编辑",
				Show: "查看",
			},
			addNum: 1,
			visible: false,
			isSaveing: false,
			needSaveFlag: false,
			keepList: ["Code"],
			fieldList: ["Code", "Name", "Remark", "State"],
			//表单数据
			entityObj: {
				Code: "",
				Code_Old: "",
				Name: "",
				Name_Old: "",
				Remark: "",
				Remark_Old: "",
				State: "1",
				State_Old: "1",
			},
			//验证规则
			rules: {
				Name: [
					{
						required: true,
						type: "string",
						message: "类型需为字符串",
					},
				],
			},
		};
	},
	methods: {
		//显示=
		open(mode = "Add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		getChangeValue() {
			// eslint-disable-next-line no-debugger
			//debugger;
			let newObj = {};
			if (this.mode == "Add") {
				this.needSaveFlag = true;
				this.fieldList.forEach((element) => {
					newObj[element] = this.entityObj[element];
				});
			} else {
				this.fieldList.forEach((element) => {
					if (
						this.entityObj[element] !=
						this.entityObj[element + "_Old"]
					) {
						newObj[element] = this.entityObj[element];
						newObj[element + "_Old"] =
							this.entityObj[element + "_Old"];
						this.needSaveFlag = true;
					}
				});
				if (this.needSaveFlag) {
					this.keepList.forEach((element) => {
						newObj[element] = this.entityObj[element];
						newObj[element + "_Old"] =
							this.entityObj[element + "_Old"];
					});
				}
			}
			// eslint-disable-next-line no-debugger
			//debugger;
			return newObj;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				// eslint-disable-next-line no-debugger
				//debugger;
				if (valid) {
					this.isSaveing = true;
					// eslint-disable-next-line no-debugger
					//debugger;
					var changeList = this.getChangeValue();
					if (!this.needSaveFlag) {
						alert("没有做修改，无需保存");
						this.isSaveing = false;
						return;
					}
					//得到的数据this.entityObj
					var head = new publicjs.head();
					publicjs.initHead(head, "save");
					var ed = new publicjs.entitydata(head);
					var eo = new publicjs.entityobj();
					eo.AttrList = {
						Name: "Role",
						DoType: this.mode,
						UseDistinct: false,
						OrgCode: "",
						UseBackUp: false,
						UseGroup: false,
					};
					//eo.ValueList = this.entityObj;
					eo.ValueList = changeList;
					if (this.mode == "Add") {
						for (let c = 0; c < this.addNum; c++)
							ed.Data.Content.ObjectList.Object.push(eo);
					} else ed.Data.Content.ObjectList.Object.push(eo);
					try {
						var returnValueStr =
							await this.$API.auth.objProcess.post(ed);
						console.log(returnValueStr);
						// eslint-disable-next-line no-debugger
						//debugger;
						//var returnValue = JSON.parse(returnValueStr);
						//alert(returnValue.opFlag);
						this.isSaveing = false;
						this.visible = false;
						this.$emit("fatherMethod");
					} catch (error) {
						this.loading = false;
						//this.emptyText = error.statusText;
						return false;
					}
				}
			});
		},

		//表单注入数据
		async setData(data) {
			//alert('aaa')
			// eslint-disable-next-line no-debugger
			////debugger
			var head = new publicjs.head();
			publicjs.initHead(head, "getobj");
			// eslint-disable-next-line no-undef
			let ed = new publicjs.entitydata(head);
			var eo = new publicjs.entityobj();
			eo.AttrList = {
				Name: "Role",
				UseDistinct: false,
				OrgCode: "",
				UseBackUp: false,
				UseGroup: false,
			};
			eo.ValueList = {
				Code: "Code",
				Name: "Name",
				Remark: "Remark",
				State: "State",
			};
			eo.QueryList = { Code: data.Code };
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				console.log(returnValueStr);
				// eslint-disable-next-line no-debugger
				//debugger;
				var returnValue = JSON.parse(returnValueStr);
				if (returnValue.Result.OpFlag == "1") {
					// eslint-disable-next-line no-debugger
					//debugger;
					let returnObj =
						returnValue.Result.Content.ObjectList.Object;
					this.entityObj.Code = this.entityObj.Code_Old =
						returnObj.ValueList.Code;
					this.entityObj.Name = this.entityObj.Name_Old =
						returnObj.ValueList.Name;
					this.entityObj.Remark = this.entityObj.Remark_Old =
						returnObj.ValueList.Remark;
					this.entityObj.State = this.entityObj.State_Old =
						returnObj.ValueList.State;
					ed = null;
					eo = null;
					head = null;
				}
				//this.summary = response.summary || {};
				this.loading = false;
				//this.$refs.scTable.setScrollTop(0);
				//this.$emit('dataChange', res, this.tableData)
			} catch (error) {
				this.loading = false;
				//this.emptyText = error.statusText;
				return false;
			}
		},
	},
	async mounted() {},
};
</script>

<style></style>
